#include "pagecontrol.h"

#include <stack.h>
#include <string.h>
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>

#define DOT_EDGE 11
#define DOT_BETWEEN 8

typedef struct _PAGECONTROL
{
	int currentPage;
	int numberOfPages;
	UFO_Stack vecDot;
}PAGECONTROL;

typedef struct _DOTDATA
{
	UFO_View subView;
	UFO_Texture normal;
	UFO_Texture highlight;
}DOTDATA;

int ufoPageControlGetCurrentPage(UFO_View view)
{
	PAGECONTROL *pPageControl = (PAGECONTROL*)ufoViewGetUserData(view);
	if (pPageControl) {
		return pPageControl->currentPage;
	}
	return 0;
}

int ufoPageControlGetNumberOfPages(UFO_View view)
{
	PAGECONTROL *pPageControl = (PAGECONTROL*)ufoViewGetUserData(view);
	if (pPageControl) {
		return pPageControl->numberOfPages;
	}
	return 0;
}

int ufoPageControlSetCurrentPage(UFO_View view, int currentPage)
{
	PAGECONTROL *pPageControl = (PAGECONTROL*)ufoViewGetUserData(view);
	if (pPageControl) {
		if (currentPage >= 0 && currentPage < pPageControl->numberOfPages) {
			DOTDATA *pData = (DOTDATA*)ufoStackGetElement(pPageControl->vecDot, pPageControl->currentPage);
			if (pData) 
				ufoViewSetBackground(pData->subView, pData->normal);

			pPageControl->currentPage = currentPage;
			
			pData = (DOTDATA*)ufoStackGetElement(pPageControl->vecDot, pPageControl->currentPage);
			if (pData) 
				ufoViewSetBackground(pData->subView, pData->highlight);

			return 1;
		}
	}
	return 0;
}

int ufoPageControlSetNumberOfPages(UFO_View view, int numberOfPages)
{
	PAGECONTROL *pPageControl = (PAGECONTROL*)ufoViewGetUserData(view);
	if (pPageControl) {

		if (pPageControl->numberOfPages < numberOfPages) {//### hard code
			pPageControl->numberOfPages = numberOfPages;

			while (numberOfPages--) {
				DOTDATA *pData = (DOTDATA*)malloc(sizeof(DOTDATA));
				memset(pData, 0, sizeof(DOTDATA));
				pData->subView = ufoViewCreate(0);
				ufoViewSetAnchor(pData->subView, ANCHOR_CENTER_VERT);
				ufoViewAddChildView(view, pData->subView);
				ufoStackPush(pPageControl->vecDot, pData);
			}
		}
		return 1;
	}
	return 0;
}

int ufoPageControlSetImage(UFO_View view, int nPage, UFO_Texture normal, UFO_Texture highlight)
{
	PAGECONTROL *pPageControl = (PAGECONTROL*)ufoViewGetUserData(view);
	if (pPageControl && nPage >= 0 && nPage < pPageControl->numberOfPages) {

		DOTDATA *pData = (DOTDATA*)ufoStackGetElement(pPageControl->vecDot, nPage);
		if (pData) {
			pData->highlight = highlight;
			pData->normal = normal;
			ufoViewSetBackground(pData->subView, normal);
			ufoPageControlSetCurrentPage(view, pPageControl->currentPage);
			return 1;
		}
	}
	return 0;
}

int default_pagecontrol_proc(UFO_View view, int id, void* wParam, void* lParam)
{
	PAGECONTROL *pPageControl = (PAGECONTROL*)ufoViewGetUserData(view);
	if (!pPageControl) {
		pPageControl = (PAGECONTROL*)malloc(sizeof(PAGECONTROL));
		memset(pPageControl, 0, sizeof(PAGECONTROL));
		ufoViewSetUserData(view, pPageControl);

		pPageControl->vecDot = ufoStackCreate(sizeof(DOTDATA));//### hard code
	}
	switch (id)
	{
	case MSG_SIZE_CHANGE:
		{
			int i = 0;
			ufoRect* rect = (ufoRect*)wParam;
			int begX = ((rect->right-rect->left)-pPageControl->numberOfPages*DOT_EDGE-(pPageControl->numberOfPages-1)*DOT_BETWEEN)/2;
			for (i = 0; i < pPageControl->numberOfPages; ++i) {
				ufoRect subRect = {begX+i*DOT_EDGE+i*DOT_BETWEEN, 0, begX+DOT_EDGE+i*DOT_EDGE+i*DOT_BETWEEN, DOT_EDGE};
				DOTDATA *pData = (DOTDATA*)ufoStackGetElement(pPageControl->vecDot, i);
				ufoViewSetRect(pData->subView, &subRect);
			}
		}
		break;

	case MSG_CREATE:
		{

		}
		break;
	}
	return ufoViewDefaultProc(view, id, wParam, lParam);//### pass to default view proc
}

